/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#include "lyricspainter.h"
#include "configmanager.h"

LyricsPainter::LyricsPainter(QObject* parent) : QObject(parent) {
    ConfigManager* cfg = ConfigManager::getInstance();

    m_font.fromString(cfg->value("Style/Font", QFont().toString()).toString());
    m_fontMetrics = new QFontMetrics(m_font);
    clearLyrics();
    m_drawState = DrawMessage;
};

LyricsPainter::~LyricsPainter() {
    delete m_fontMetrics;
};


void LyricsPainter::draw(QPaintDevice* device, LyricsTime time) {
    ConfigManager* cfg = ConfigManager::getInstance();

    QPainter painter;
    m_device = device;
    if (m_rect.isNull())
        m_rect.setRect(0, 0, device->width(), device->height());

    if (painter.begin(device)) {
        painter.setFont(m_font);
        painter.setClipRect(m_rect);
        // fill the background first
        //m_painter.setCompositionMode(QPainter::CompositionMode_Source);
        //QPixmap back = QPixmap::grabWindow(0);
        //m_painter.fillRect(rect(), Qt::transparent);
        //m_painter.fillRect(e->rect(), QBrush(cfg->value("Style/BgColor", Qt::black).value<QColor>()));
        //m_painter.drawPixmap(0, 0, back);
        switch (m_drawState) {
        case DrawMessage:
            drawMessage(painter, m_msg);
            break;
        case DrawLyrics:
            drawLyrics(painter, time); 
            break;
        };
        painter.end();
    };
    /*SIZE sz = {rect().width(), rect().height()};
    POINT ptSource = {0};
    BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
    HDC hDC = getDC();

    UpdateLayeredWindow(parentWidget()->winId(), NULL, NULL, &sz, hDC,
        &ptSource, 0, &bf, ULW_ALPHA);
    releaseDC(hDC);
    */
};

QColor LyricsPainter::alphaBlending(const QColor& c1, const QColor& c2, const int factor) {
    return QColor((c1.red() * factor + c2.red() * (0xff - factor)) >> 8,
        (c1.green() * factor + c2.green() * (0xff - factor)) >> 8,
        (c1.blue() * factor + c2.blue() * (0xff - factor)) >> 8);
};

QColor LyricsPainter::getFadingColor(const int timePass, const int timeLength) {
    ConfigManager* cfg = ConfigManager::getInstance();
    if (!cfg->value("Style/EnableFading", true).toBool() || timeLength == 0)
        return cfg->value("Style/HighlightColor", Qt::green).value<QColor>();

    int factor = (timePass * 2 <= timeLength) ? timePass : timeLength - timePass;
    factor = qMin(0xff, factor * 0xff / cfg->value("Style/FadingTime", 300).toInt());
    return alphaBlending(
        cfg->value("Style/HighlightColor", Qt::green).value<QColor>(),
        cfg->value("Style/FgColor", Qt::darkGreen).value<QColor>(),
        factor);
};

void LyricsPainter::drawMessage(QPainter& painter, const QString& msg) {
    ConfigManager* cfg = ConfigManager::getInstance();

    drawText(painter,
        m_rect.left() + (m_rect.width() - m_fontMetrics->width(msg)) / 2,
        m_rect.top() + (m_rect.height() - m_fontMetrics->height()) / 2 + m_fontMetrics->ascent(),
        cfg->value("Style/HighlightColor", Qt::green).value<QColor>(),
        msg);
};

void LyricsPainter::drawText(QPainter& painter, int x, int y, const QColor& color, const QString& text) {
    ConfigManager* cfg = ConfigManager::getInstance();
    /*painter.setPen(cfg->value("Style/ShadowColor", Qt::gray).value<QColor>());
    painter.drawText(x + 1, y + 1, text);
    painter.drawText(x - 1, y - 1, text);
    painter.drawText(x - 1, y, text);
    painter.drawText(x + 1, y + 1, text);
    painter.drawText(x, y + 1, text);
    */
    painter.setPen(color);
    painter.drawText(x, y, text);
};

void LyricsPainter::updateInterval(int pixels, int timeLength) {
    ConfigManager* cfg = ConfigManager::getInstance();
    int interval = int(double(timeLength) / pixels + 0.5);
    int minInterval = cfg->value("Advanced/MinimalUpdateInterval", 50).toInt();
    if (interval == 0)
        interval = minInterval;
    if (interval < minInterval) {
        interval = (minInterval / interval + 1) * interval;
    } else if (interval > minInterval) {
        interval = interval / (interval / minInterval);
    };

    emit intervalChanged(interval);
    //return interval;
};

